package com.wnxy.controller;

import com.wnxy.config.AliyunTemplete;
import com.wnxy.dto.Result;
import com.wnxy.vo.DeletePhotoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.convert.Jsr310Converters;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.util.calendar.BaseCalendar;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.util.Date;

@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private AliyunTemplete aliyunTemplete;
    @RequestMapping("/upload")
    public Result upload(MultipartFile mf) throws IOException {
        String buckName="aliceache";
        String filename = mf.getOriginalFilename();
        String contentType=filename.substring(filename.lastIndexOf("."));
        LocalDate now = LocalDate.now();
        filename=now.getYear()+"/"+now.getMonthValue()+"/"+System.currentTimeMillis()+contentType;
        InputStream is = mf.getInputStream();
        System.out.println(filename);
        String url = aliyunTemplete.upload(buckName, filename, is);
        return Result.getInstance().setFlag(true)
                .setStatusCode(200)
                .setData(url);
    }
    @RequestMapping("/remove")
    public Result remove( @RequestBody DeletePhotoVo deletePhotoVo) throws IOException {
        String buckName="aliceache";
        System.out.println(deletePhotoVo.getFilename());
        aliyunTemplete.delete(buckName,deletePhotoVo.getFilename());
        return Result.getInstance().setFlag(true)
                .setStatusCode(200)
                .setData(null);
    }
}
